package com.spica.platform.base.model;

import com.spica.platform.base.exception.errorcode.RtnCodeEnum;
import lombok.Getter;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 返回结果封装
 */
@Getter
public class ResponseEntity<T> extends org.springframework.http.ResponseEntity<T> implements Serializable {

	private String rtnCod;
	private String rtnMsg;
	private T data;

	public ResponseEntity(String rtnCod, String rtnMsg, T data) {
		super(HttpStatus.INTERNAL_SERVER_ERROR);
		this.rtnCod = rtnCod;
		this.rtnMsg = rtnMsg;
		this.data = data;
	}

	public ResponseEntity(HttpStatus status) {
		super(status);
		this.rtnCod = String.valueOf(status.value());
		this.rtnMsg = status.getReasonPhrase();
	}
	/**
	 * 只有返回消息，没有返回数据的成功码
	 * @param rtnMsg 返回消息
	 * @return 返回结果
	 */
	public static <T> ResponseEntity<T> succeed(String rtnMsg) {
		return of(RtnCodeEnum.SUCCESS.getRtnCod(), rtnMsg, null);
	}


	public static <T> ResponseEntity<T> succeed(String rtnMsg, T data) {
		return of(RtnCodeEnum.SUCCESS.getRtnCod(), rtnMsg, data);
	}

	public static <T> ResponseEntity<T> succeed(T data) {
		return of(RtnCodeEnum.SUCCESS.getRtnCod(), "", data);
	}

	public static <T> ResponseEntity<T> of(String rtnCod, String rtnMsg, T data) {
		return new ResponseEntity<>(rtnCod, rtnMsg, data);
	}

	public static <T> ResponseEntity<T> of(int rtnCod, String rtnMsg, T data) {
		return new ResponseEntity<>(String.valueOf(rtnCod), rtnMsg, data);
	}

	public static <T> ResponseEntity<T> failed(String rtnMsg) {
		return of(RtnCodeEnum.FAIL.getRtnCod(), rtnMsg, null);
	}

	public static <T> ResponseEntity<T> failed(String rtnMsg, T data) {
		return of(RtnCodeEnum.FAIL.getRtnCod(), rtnMsg, data);
	}
	
	public static <T> ResponseEntity<T> succeedWith(String rtnCod, String rtnMsg, T data) {
		return new ResponseEntity<T>(rtnCod, rtnMsg , data);
	}

	public static <T> ResponseEntity<T> succeedWith(int rtnCod, String rtnMsg, T data) {
		return new ResponseEntity<T>(String.valueOf(rtnCod), rtnMsg , data);
	}

	public static <T> ResponseEntity<T> failedWith(String rtnCod, String rtnMsg, T data) {
        return new ResponseEntity<T>(rtnCod, rtnMsg , data);
    }

	public static <T> ResponseEntity<T> failedWith(int rtnCod, String rtnMsg, T data) {
		return new ResponseEntity<T>(String.valueOf(rtnCod), rtnMsg , data);
	}
}
